import { createSlice } from '@reduxjs/toolkit';

export const counterSlice = createSlice({
    // 命名空间
    name: 'counter',
    // 初始状态
    initialState: {
        count: 1,
        title: 'redux toolkit pre'
    },
    // reducers
    reducers: {
        increment(state, { payload }) {
            state.count = state.count + payload.step;
        },
        decrement(state) {
            state.count -= 1;
        }
    }
})

export const { increment, decrement } = counterSlice.actions;

// 异步
export const asyncIncrement = (payload) => (dispatch) => {
    setTimeout(() => {
        dispatch(increment(payload));
    }, 2000);
}

export default counterSlice.reducer;